/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package View;

import javax.swing.*;


/**
 *
 * @author CCD gruppe 2.
 */
public class ChatPanel extends JPanel {

    private javax.swing.JLabel chatRoomForProjectLabel;
    private javax.swing.JTextArea inputText;
    private javax.swing.JScrollPane inputTextScrollPane;
    private javax.swing.JScrollPane mainChatPane;
    private javax.swing.JLabel onlineUsersLabel;
    private javax.swing.JButton sendButton;
    private javax.swing.JTextArea textAreaMainChat;
    private javax.swing.JList userList;
    private javax.swing.JScrollPane userScrollPane;
    private DefaultListModel listModel;


    public ChatPanel() {
            initComponents();

    }

    public void initComponents() {

        listModel = new DefaultListModel();
        mainChatPane = new javax.swing.JScrollPane();
        textAreaMainChat = new javax.swing.JTextArea();
        userScrollPane = new javax.swing.JScrollPane();
        userList = new javax.swing.JList();
        onlineUsersLabel = new javax.swing.JLabel();
        chatRoomForProjectLabel = new javax.swing.JLabel();
        inputTextScrollPane = new javax.swing.JScrollPane();
        inputText = new javax.swing.JTextArea();
        sendButton = new javax.swing.JButton();

        userList.setModel(listModel);
        inputText.setAutoscrolls(false);
        textAreaMainChat.setColumns(20);
        textAreaMainChat.setEditable(false);
        textAreaMainChat.setLineWrap(true);
        textAreaMainChat.setRows(5);
        textAreaMainChat.setCaretPosition(textAreaMainChat.getDocument().getLength());

        mainChatPane.setViewportView(textAreaMainChat);

        userScrollPane.setViewportView(userList);

        userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        onlineUsersLabel.setText("Påloggede brukere:");


        chatRoomForProjectLabel.setText("Chatterom for prosjekt:  ");

        inputText.setColumns(20);
        inputText.setLineWrap(true);
        inputText.setRows(3);
        inputTextScrollPane.setViewportView(inputText);

        sendButton.setText("Send");
        inputText.setFocusable(false);
        sendButton.setEnabled(false);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(inputTextScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
                    .add(mainChatPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
                    .add(chatRoomForProjectLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(sendButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(userScrollPane, 0, 0, Short.MAX_VALUE)
                    .add(onlineUsersLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(chatRoomForProjectLabel)
                    .add(onlineUsersLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(mainChatPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, userScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(inputTextScrollPane)
                    .add(sendButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE))
                .addContainerGap())
        );
        setVisible(true);

    }

    public JLabel getChatRoomForProjectLabel() {
        return chatRoomForProjectLabel;
    }

    public JTextArea getInputText() {
        return inputText;
    }

    public JScrollPane getInputTextScrollPane() {
        return inputTextScrollPane;
    }

    public DefaultListModel getListModel() {
        return listModel;
    }

    public JScrollPane getMainChatPane() {
        return mainChatPane;
    }

    public JLabel getOnlineUsersLabel() {
        return onlineUsersLabel;
    }

    public JButton getSendButton() {
        return sendButton;
    }

    public JTextArea getTextAreaMainChat() {
        return textAreaMainChat;
    }

    public JList getUserList() {
        return userList;
    }

    public JScrollPane getUserScrollPane() {
        return userScrollPane;
    }

    public void activateChat(){
        inputText.setFocusable(true);
        sendButton.setEnabled(true);
    }

    public void appendLogToTextAreaMainChat(String log){
        this.textAreaMainChat.append(log);
    }

    public void appendMoreLogToTextAreaMainChat(String log){
        String line = "\n--------------------------------------------------------------------------\n";
        String logmessage = "\nOvenfor er logg hentet fra databasen!\n";
        this.textAreaMainChat.append(log+line+logmessage);
    }
    public void clearTextAreaMainChat(){
        textAreaMainChat.setText("");
    }

}

